package com.hgyc.mom.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hgyc.mom.base.entity.MaterialType;
import com.hgyc.mom.base.entity.ProductSeries;
import com.hgyc.mom.base.entity.Unit;
import com.hgyc.mom.base.service.MaterialTypeService;
import com.hgyc.mom.base.service.ProductSeriesService;
import com.hgyc.mom.base.service.UnitService;
import com.hgyc.mom.base.vo.MaterialImportVO;
import com.hgyc.mom.common.enums.CommonStatus;
import com.hgyc.mom.common.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hgyc.mom.base.mapper.MaterialMapper;
import com.hgyc.mom.base.entity.Material;
import com.hgyc.mom.base.service.MaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 物料基础信息Service业务层处理
 *
 * @author fwj
 * @since 2025-04-28
 */
@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements MaterialService
{

    @Autowired
    private MaterialTypeService materialTypeService;

    @Autowired
    private UnitService unitService;

    @Autowired
    private ProductSeriesService productSeriesService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importData(List<MaterialImportVO> materialList) {
        List<Material> insertList = new ArrayList<>();
        List<Material> updateList = new ArrayList<>();
        for (MaterialImportVO materialImportVO : materialList) {
            Material material = new Material();
            BeanUtils.copyProperties(materialImportVO, material);

            //类型
            String typeName = materialImportVO.getType();
            if (StringUtils.isNotEmpty(typeName)) {
                MaterialType materialType = materialTypeService.getByName(typeName);
                if (materialType != null) {
                    material.setTypeId(materialType.getId());
                }
            }

            //系列
            String seriesName = materialImportVO.getSeries();
            if (StringUtils.isNotEmpty(seriesName)) {
                ProductSeries productSeries = productSeriesService.getByName(seriesName);
                if (productSeries != null) {
                    material.setSeriesId(productSeries.getId());
                }
            }

            //单位
            String unitName = materialImportVO.getUnit();
            if (StringUtils.isNotEmpty(unitName)) {
                Unit materialUnit = unitService.getByName(unitName);
                if (materialUnit != null) {
                    material.setUnitId(materialUnit.getId());
                }
            }

            material.setStatus(CommonStatus.ENABLE.getValue());

            //更新或新增
            String materialCode = materialImportVO.getMaterialCode();
            Material existMaterial = this.getByCode(materialCode);
            if (existMaterial != null) {
                updateList.add(material);
            } else {
                insertList.add(material);
            }
        }

        if (!insertList.isEmpty()) {
            saveBatch(insertList);
        }
        if (!updateList.isEmpty()) {
            updateBatchById(updateList);
        }

        return "";
    }

    @Override
    public Material getByCode(String code) {
        LambdaQueryWrapper<Material> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Material::getMaterialCode, code);
        return getOne(queryWrapper, false);
    }
}
